package com.hp.itsm.api.examples;

import com.hp.itsm.api.*;
import com.hp.itsm.api.interfaces.*;
import java.util.Hashtable;
import java.util.Enumeration;

/**
 * Show how to get all available views for service calls.
 * Query all records that belong to the standard view.
 *
 * @version $Revision: 3 $
 */
public class Example6
{
    /**
     * arguments :
     * server		: the server to connect to
     * username		: the username of the account you want to use
     * password		: the password of the account you want to use
     */
    public static void main(String args[]) {
        if (args.length != 3) {
            System.err.println("Usage : Example6 <server> <username> <password>");
            return; 
        }
        String server = args[0];
        String username = args[1];
        String password = args[2];
        ApiSDSession session = null;
        
        try {
            session = ApiSDSession.openSession(server, username, password);
        } catch (RuntimeException e) {
            // Something went wrong while logging on, show error
            System.out.println(e.getMessage());
            return;
        }
        
        // get the home of service call
        IServicecallHome scHome = session.getServicecallHome();
        
        // get all views that are defined for service call
        Hashtable scViews = scHome.getServicecallViews();
        
        // show the id's and names of all views
        for (Enumeration enumeration = scViews.keys(); enumeration.hasMoreElements();) {
            Long viewId = (Long) enumeration.nextElement();
            String viewName = (String) scViews.get(viewId);
            System.out.println("" + viewId + " : " + viewName);
        }
        
        // get the default view
        Long defaultViewId = scHome.getDefaultView();
        if (defaultViewId != null) {
            String defaultView = (String) scViews.get(defaultViewId);
            System.out.println("The default view for service calls is '" + defaultView + "'.");
        } else { // there is no default view, just take the first one
            System.out.println("No default View");
            defaultViewId = (Long) scViews.keys().nextElement();
        }
        
        // get all service calls of this view
        IServicecall[] servicecalls = scHome.findServicecall(defaultViewId);
        
        // loop through all these service calls, and show the id and description.
        for (int i = 0; i < servicecalls.length; i++) {
            String description = servicecalls[i].getDescription();
            Long id = servicecalls[i].getID();
            System.out.println("Servicecall " + id + " : " + description);
        }
    }

    public static final String REVISION = "$Revision: 3 $";
}
